package com.leostadyn.stackfarm.timestatistics.net.base;

import com.google.gson.JsonParseException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.HttpException;

public class ExceptionHandler {

    public static ResponseThrowable handleException(Throwable e) {
        ResponseThrowable ex = null;
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ResponseThrowable("XT001", "网络异常，请重新尝试");
            switch (httpException.code()) {
                case 404:
                    ex.setMessage("");
                    break;
                default:
                    ex.setMessage(e.getMessage());
                    break;
            }
            ex.setCode(String.valueOf(httpException.code()));
            return ex;
        } else if (e instanceof SocketTimeoutException) {
            ex = new ResponseThrowable("XT002", "连接超时，请重新尝试");
//            ex.setMessage(BaseApplication.getA().getString(R.string.comm_conce_over));
        } else if (e instanceof JsonParseException) {
            ex = new ResponseThrowable("XT003", "数据解析失败，核对你的实体Bean");
//            ex.setMessage("数据解析失败，请稍后再试");
//            ex.setMessage(BaseApplication.getA().getString(R.string.comm_conce_over));
        } else if (e instanceof ConnectException || e instanceof UnknownHostException) {
            ex = new ResponseThrowable("XT004", "网络异常，请重新尝试");
//            ex.setMessage(BaseApplication.getA().getString(R.string.comm_conce_errow));
        } else if (e instanceof ResponseThrowable) {
            ex = (ResponseThrowable) e;
//            if (((ResponseThrowable) e).getCode() == 10001) {
//                try {
//                    Intent intent = new Intent("tokenAbate");
//                    LocalBroadcastManager.getInstance(HttpService.getContext()).sendBroadcast(intent);
//                } catch (Exception e1) {
//                }
//            }
        } else {
            ex = new ResponseThrowable("XT005", "网络异常，请重新尝试");
//            ex.setMessage(BaseApplication.getA().getString(R.string.comm_conce_errow));
            ex.setMessage(e.getMessage());
        }
        return ex;
    }

}
